/*
 * seqLettersFreq.cpp
 *
 *  Created on: 2011-09-28
 *      Author: pawel
 */
#include"seqLettersFreq.h"
#include<Bpp/Seq/Sequence.h>
#include"smart2DArray.h"

#include<stdio.h>
seqLettersFreq::seqLettersFreq(bpp::BasicSequence *seqPtr)
{
	alphabetPtr = seqPtr->getAlphabet();
	numOfLetters = (size_t)(alphabetPtr->getNumberOfTypes() +1);
	lettersFreqVec.reserve(numOfLetters);
	seqLen = seqPtr->getContent().size();
	smart2DArray<int> seqContentSmartArray(1,seqLen);
	int *seqContentArray = (seqContentSmartArray.getPtr())[0];
	memcpy((void*)seqContentArray,&((seqPtr->getContent())[0]),seqLen*sizeof(int));

	size_t *vecArray = &(lettersFreqVec[0]);
	memset(vecArray,0,numOfLetters*sizeof(int));
	int letter(0);
	for(size_t i=0;i<seqLen;i++){
		letter = seqContentArray[i];
		if(letter>=0){
			vecArray[letter]++;
		}
	}

}
std::vector<size_t>& seqLettersFreq::getFreqVectorRef(){
	return lettersFreqVec;
}
bpp::BasicSequence* seqLettersFreq::getSimpleSeq(){
	std::vector<int> seqContent;
	seqContent.reserve(seqLen);
	size_t pos=0;
	for(size_t i = 0;i<numOfLetters;i++){
		for(size_t j =0;j<lettersFreqVec[i];j++){
			seqContent[pos++] = (int)i;
		}
	}
	std::string stringg = "";
	stringg.reserve(seqLen);
	for(size_t i = 0 ;i<seqLen;i++){
				stringg+= alphabetPtr->intToChar(seqContent[i]);
			}

	return  new bpp::BasicSequence("Simple seq",stringg,alphabetPtr);

}
